#ifndef _SRC_SERVER_BLOG_CACHER_H_
#define _SRC_SERVER_BLOG_CACHER_H_

#include <filesystem>
#include <map>
#include <memory>
#include <mutex>
#include <shared_mutex>
#include <string>
#include <string_view>

#include "src/net/timer_id.h"

namespace fio66 {
namespace net {
class EventLoop;
}  // namespace net

static constexpr double kUpdateInterval = 60.0;

class BlogCacher {
 public:
  struct FileInfo {
    std::string_view file_type;
    std::string file_content;
  };

  BlogCacher(net::EventLoop* event_loop, std::string_view uri_path);
  ~BlogCacher();

  bool Start();

  std::shared_ptr<FileInfo> GetFileInfo(const std::filesystem::path& path);

 private:
  void UpdateCache();
  void UpdateFile(const std::filesystem::path& path);

 private:
  bool is_start_ = false;
  net::EventLoop* event_loop_ = nullptr;
  net::TimerId timer_id_;
  std::string uri_path_;
  std::shared_mutex mu_;
  std::map<std::string, std::filesystem::file_time_type> file_cache_;
  std::map<std::filesystem::path, std::shared_ptr<FileInfo>> blog_file_;
};

}  // namespace fio66

#endif  // _SRC_SERVER_BLOG_CACHER_H_
